package main

import (
	"flag"
	// "github.com/k0kubun/pp"
	"github.com/NYTimes/gziphandler"
	"log"
	"net/http"
	"strings"
)

func init() {
	log.SetFlags(log.Lshortfile)
}

var chttp = http.NewServeMux()

func main() {
	dir := flag.String("dir", ".", "")
	port := flag.String("port", ":8000", "")
	flag.Parse()
	chttp.Handle("/", http.FileServer(http.Dir(*dir)))
	http.HandleFunc("/", HomeHandler)
	log.Println("Listening on http://0.0.0.0" + *port)
	log.Println("Dir:", *dir)
	log.Fatalln(http.ListenAndServe(*port, nil))
}

func HomeHandler(w http.ResponseWriter, r *http.Request) {
	if strings.Contains(r.URL.RawQuery, "@cache") {
		w.Header().Set("Cache-Control", "max-age=86400")
	}
	if strings.Contains(r.URL.RawQuery, "@gzip") {
		gziphandler.GzipHandler(chttp).ServeHTTP(w, r)
	} else {
		chttp.ServeHTTP(w, r)
	}
}
